package com.wandian.weather.mvvm.moredayweather.pagefragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.R;
import com.wandian.weather.adapter.New30DaysCalendarAdapter;
import com.wandian.weather.bean.CalendarItemBean;
import com.wandian.weather.bean.TianqiWeather30Bean;
import com.wandian.weather.databinding.WeatherFragmentCalendarViewBinding;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/16-13:30
 * @description ChildPageFragment
 **/
public class ChildPageFragment extends BaseMVVMFragment<ChildPageViewModel> implements New30DaysCalendarAdapter.OnCalendarClickListener{

    private WeatherFragmentCalendarViewBinding dataBinding;
    private New30DaysCalendarAdapter calendarAdapter;
    private int index;
    private String title;
    private String cityID;
    private TianqiWeather30Bean weather30Bean;
    private List<CalendarItemBean> listCalendar = new ArrayList<>();
    public static ChildPageFragment newInstance(String title,String cityID) {
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("cityID", cityID);
        ChildPageFragment fragment = new ChildPageFragment();
        fragment.setArguments(args);
        return fragment;
    }
    private CalendarItemClink calendarItemClink;

    public CalendarItemClink getCalendarItemClink() {
        return calendarItemClink;
    }

    public void setCalendarItemClink(CalendarItemClink calendarItemClink) {
        this.calendarItemClink = calendarItemClink;
    }

    public interface CalendarItemClink{
        void itemOneDayData(String dayNumber);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            if(calendarItemClink == null) calendarItemClink = (CalendarItemClink) activity;
        } catch (ClassCastException e) {

        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = WeatherFragmentCalendarViewBinding.inflate(inflater);
        dataBinding.setLifecycleOwner(this);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        dataBinding.setViewModel(mViewModel);
        title = getArguments().getString("title")+"";
        cityID = getArguments().getString("cityID");
        initViewData();
        if(!UIUtils.isEmpty(cityID))mViewModel.getWeatherData(cityID);
    }

    private void initViewData(){
        mViewModel.tianqiWeather30Bean.observe(this,bean->{
            if(!UIUtils.isEmpty(bean)){
                this.weather30Bean =bean;
                if (title.equals("1")) {
                    index = 0;
                    initCalenerView();
                } else{
                    index =1;
                    initCalenerView2();
                }
            }
        });
    }

    private void initCalenerView() {
        try {
            addCalendarWeekString();
            Calendar calendarMin = Calendar.getInstance();

            int week = calendarMin.get(Calendar.DAY_OF_WEEK);
            int index_b = week - 1;

            for (int i = index_b; i > 0; i--) {
                Calendar c = Calendar.getInstance();
                c.add(Calendar.DAY_OF_MONTH, 0 - i);
                int day = c.get(Calendar.DAY_OF_MONTH);
                int month = c.get(Calendar.MONTH) + 1;
                String date = "";
                if (month < 10)
                    date = "0" + month + "/" + (day < 10 ? "0" + day : day);
                else date = month + "/" + (day < 10 ? "0" + day : day);
                CalendarItemBean c1 = new CalendarItemBean(String.valueOf(day), false, true, date);
                listCalendar.add(c1);
            }

            int size = 28 - index_b;
            for (int i = 0; i < size; i++) {
                Calendar c = Calendar.getInstance();
                c.add(Calendar.DAY_OF_MONTH, i);
                int day = c.get(Calendar.DAY_OF_MONTH);
                int month = c.get(Calendar.MONTH) + 1;
                String date = "";
                if (month < 10)
                    date = "0" + month + "/" + (day < 10 ? "0" + day : day);
                else date = month + "/" + (day < 10 ? "0" + day : day);
                CalendarItemBean c1 = new CalendarItemBean(String.valueOf(day), false, false, date);
                if (i == 0) c1.setToday(true);
                listCalendar.add(c1);
            }

            List<TianqiWeather30Bean.WeatherDaysBean> weatherDaysBeans = weather30Bean.getWeather_days();
            for (CalendarItemBean cib : listCalendar) {
                if (cib.isWeek() || cib.isOld()) continue;
                for (TianqiWeather30Bean.WeatherDaysBean wdb : weatherDaysBeans) {
                    if (TextUtils.equals(cib.getaDate(), wdb.getCdate())) {
                        cib.setImgNum(wdb.getImg_num());
                        break;
                    }
                }
            }

            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 7);
            dataBinding.calendar30days.setLayoutManager(gridLayoutManager);
            calendarAdapter = new New30DaysCalendarAdapter(R.layout.weather_item_calendar_30days, listCalendar);
            calendarAdapter.initChoosPage(index);
            calendarAdapter.setOnCalendarClickListener(this);
            dataBinding.calendar30days.setAdapter(calendarAdapter);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initCalenerView2() {
        try {
            addCalendarWeekString();
            Calendar calendarMin = Calendar.getInstance();
            int month = calendarMin.get(Calendar.MONTH) + 1;

            calendarMin.add(Calendar.MONTH, 1);
            calendarMin.set(Calendar.DAY_OF_MONTH, 1);
            int week = calendarMin.get(Calendar.DAY_OF_WEEK);
            int index_b = week - 1;
            for (int i = index_b; i > 0; i--) {
                Calendar c = Calendar.getInstance();
                c.add(Calendar.MONTH, 1);
                c.set(Calendar.DAY_OF_MONTH, 1);
                c.add(Calendar.DAY_OF_MONTH, 0 - i);
                int day = c.get(Calendar.DAY_OF_MONTH);
                int m3 = c.get(Calendar.MONTH) + 1;
                String date = "";
                if (m3 < 10)
                    date = "0" + m3 + "/" + (day < 10 ? "0" + day : day);
                else date = m3 + "/" + (day < 10 ? "0" + day : day);
                CalendarItemBean c1 = new CalendarItemBean(String.valueOf(day), false, false, date);
                listCalendar.add(c1);
            }

            for (int i = 0; i < 30; i++) {
                Calendar c = Calendar.getInstance();
                c.add(Calendar.DAY_OF_MONTH, i);
                int m2 = c.get(Calendar.MONTH) + 1;
                if (month == m2) continue;

                int day = c.get(Calendar.DAY_OF_MONTH);
                String date = "";
                if (m2 < 10)
                    date = "0" + m2 + "/" + (day < 10 ? "0" + day : day);
                else date = m2 + "/" + (day < 10 ? "0" + day : day);
                CalendarItemBean c1 = new CalendarItemBean(String.valueOf(day), false, false, date);
                listCalendar.add(c1);
            }

            List<TianqiWeather30Bean.WeatherDaysBean> weatherDaysBeans = weather30Bean.getWeather_days();
            for (CalendarItemBean cib : listCalendar) {
                if (cib.isWeek() || cib.isOld()) continue;
                for (TianqiWeather30Bean.WeatherDaysBean wdb : weatherDaysBeans) {
                    if (TextUtils.equals(cib.getaDate(), wdb.getCdate())) {
                        cib.setImgNum(wdb.getImg_num());
                        break;
                    }
                }
            }

            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 7);
            dataBinding.calendar30days.setLayoutManager(gridLayoutManager);
            calendarAdapter = new New30DaysCalendarAdapter(R.layout.weather_item_calendar_30days, listCalendar);
            calendarAdapter.initChoosPage(index);
            calendarAdapter.setOnCalendarClickListener(this);
            dataBinding.calendar30days.setAdapter(calendarAdapter);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addCalendarWeekString() {
        CalendarItemBean c1 = new CalendarItemBean("周日", true, true, "");
        CalendarItemBean c2 = new CalendarItemBean("周一", true, true, "");
        CalendarItemBean c3 = new CalendarItemBean("周二", true, true, "");
        CalendarItemBean c4 = new CalendarItemBean("周三", true, true, "");
        CalendarItemBean c5 = new CalendarItemBean("周四", true, true, "");
        CalendarItemBean c6 = new CalendarItemBean("周五", true, true, "");
        CalendarItemBean c7 = new CalendarItemBean("周六", true, true, "");
        listCalendar.add(c1);
        listCalendar.add(c2);
        listCalendar.add(c3);
        listCalendar.add(c4);
        listCalendar.add(c5);
        listCalendar.add(c6);
        listCalendar.add(c7);
    }

    @Override
    public void calendarClick(CalendarItemBean item, int pageNum) {
        Log.d("CalendarItemBean", pageNum+"="+JsonUtil.toJson(item)+"");
        if(!UIUtils.isEmpty(calendarItemClink) && !UIUtils.isEmpty(item)){
            calendarItemClink.itemOneDayData(item.getaDate()+"");
        }
    }
}
